<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('books', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
            $table->string('title');
            $table->string('author');
            $table->string('publisher');
            $table->string('isbn')->nullable();
            $table->string('category');
            $table->text('description');
            $table->string('image')->nullable();
            $table->decimal('price', 10, 2);
            
            // 添加外键字段，关联用户（上传者/管理员）
            $table->unsignedBigInteger('user_id')->nullable();
            $table->foreign('user_id')->references('id')->on('users')->onDelete('set null');
            
            // 添加软删除功能
            $table->softDeletes();
            
            // 为常用查询字段添加索引
            $table->index('author');
            $table->index('category');
            $table->index('created_at');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('books');
    }
};